package networktool;

import drinkkiosk.CardPanel;
import drinkkiosk.drinks.SpecialDrink;
import java.awt.CardLayout;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author novaterata
 */
public class KioskServer implements Runnable {

    private Socket server;
    private SpecialDrink special;
    private CardPanel cardPanel;

    /**
     * 
     * @param server
     * @param cardPanel
     */
    public KioskServer(Socket server, CardPanel cardPanel) {
        this.server = server;
	this.cardPanel = cardPanel;
    }

    @Override
    public void run() {

        special = null;

        try {
            // Get input from the client
            DataInputStream in = new DataInputStream(server.getInputStream());
            ObjectInputStream ois = new ObjectInputStream(in);
            PrintStream out = new PrintStream(server.getOutputStream());

            while ((special = (SpecialDrink) ois.readObject()) != null) {
                if("offline".equals(special.getName())){
                    cardPanel.showSynchronized("offline");
                }
                if("online".equals(special.getName())){
                    cardPanel.showSynchronized("welcome");
                }
            }


            server.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(KioskServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ioe) {
        }
    }
}